public class LinkedList <E>{
    private ListNode<E> head,tail;
    private int size;
    public LinkedList(){
        head = null;
        tail = null;
    }
    public void add(E e){
        ListNode<E> node=new ListNode<>(e);
        size++;
        if(tail==null)  head=node;
        else            tail.next=node;
        tail=node;
    }
    public String toString(){
        StringBuilder str=new StringBuilder();
        ListNode<E> node=head;
        do{
//            System.out.print(node.data);
            str.append(node.data);
        }while((node=node.next)!=null);
        return str.toString();
    }
    public void reverse() {
        ListNode<E> current=head, next,prev=null;
        next=head;
        head=tail;
        tail=next;
        next=null;
        while(true){
            if(current==null) break;
            next=current.next;
            current.next=prev;
            prev=current;
            current=next;
        }
    }

    public static void main(String[] args) {
        LinkedList <Integer> list=new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.reverse();
        System.out.println(list);
    }
}
class ListNode<E> {
    E data;
    ListNode<E> next;
    public ListNode(E data){
        this.data=data;
        this.next=null;
    }

    @Override
    public String toString() {
        return data.toString()+",";
    }
}
